// 审批流按钮事件脚本
import layerFun from '@/common/comFun/layerFun.js';
var layer = layerFun;

function ApprovalFun() {
	return {
		startApprove: function(vm) { // 发起审批
			//判定是否启用审批流
			if (vm.approvalConfig.isEnableApproval) {
				// 二开配置项数据
				let globalData = vm.globalData.ds[0];

				//判断审批流的类型
				var approvalSql = "select * from SYS_BillAPFlow a where isnull(Type,'')!='999' and APPFormID=" +
					globalData
					.Id + "  ";
				// //查询审批记录权限

				vm.Request.requestByUrl("/SQLData/QuerySql", {
					sql: approvalSql
				}, function(data) {
					const apType1Items = data.ds.filter(item => item.APType == 1);
					let filterData = apType1Items.length > 0 ? apType1Items : data.ds.filter(item => item
						.APType == 2);
					if (filterData.length) {
						ApprovalFun().configApprove(vm, filterData);
					} else {
						layer.msg('该单据暂未配置审批流方式');
						vm.enabledSubmitBtn = true;
					};
				});
			} else {
				layer.msg('该单据暂未开启审批流');
				vm.enabledSubmitBtn = true;
			};
		},
		configApprove(vm, approveInfo) { // 根据APType启用不同的审批流

			/**
			 * approveInfo.APType 是否固定审批流  1--固定审批流  or 非固定式审批流
			 * globalData 二开配置项数据
			 */
			let globalData = vm.globalData.ds[0];
			if (approveInfo[0].APType == 1) { // 固定审批流
				vm.$popup.show({
					option: approveInfo, // 打开的选项弹层的数据源
					globalData: globalData,
					keyName: vm.keyName,
					keyValue: vm.keyValue,
					confirm: () => {
						let workConfig = vm.approvalConfig.workConfig;

						let work = workConfig[0];

						var script = work.script.replace(/\['/g, "vm.formData['");
						script = script.replace(/\["/g, "vm.formData[\"");
						eval(script);
						// 刷新页面

						vm.refreshPage();
						// vm.initConfig(vm.detailName);

						// // 编辑页触发详情页刷新
						// uni.$emit('FormInitToRefresh', {
						// 	keyID: vm.formData[vm.keyName]
						// })
					},
					cancel: () => {

						vm.enabledSubmitBtn = true;
					}
				})
			} else {
				let approveObj = vm.$utils.deepClone(vm.$store.state.st_comPageConfig.approveObj);

				approveObj['workFlowDef']['name'] = approveInfo[0].BillName;

				Vue.prototype.$store.commit('ChangeAndSetStateData', {
					Name1: 'BaseScreenParm',
					data: {
						option: approveInfo,
						globalData: globalData,
						keyName: vm.keyName,
						keyValue: vm.keyValue,
						approveObj: approveObj,
					}
				})

				uni.navigateTo({
					url: '/uni_modules/mc-ui/components/m-node-warp/compontentPage/m-globalPopup/m-nodeWarpPage',
					events: {
						confirm: function(data) {

						},
						doneConfirm: (data) => { // 触发按钮的加载完事件脚本

							let workConfig = vm.approvalConfig.workConfig

							let work = workConfig[data];
							console.log(work);

							var script = work.script.replace(/\['/g,
								"vm.formData['");
							script = script.replace(/\["/g, "vm.formData[\"");
							console.log(script);
							eval(script);
							// 刷新页面
							// vm.refreshPage();

							// vm.initConfig(vm.detailName);

							// // 编辑页触发详情页刷新
							// uni.$emit('FormInitToRefresh', {
							// 	keyID: vm.formData[vm.keyName]
							// })
						},
						cancel: function(data) {
							vm.enabledSubmitBtn = true;

						}
					}
				})
			}


		},
		formApprove: function(vm) { // 审批单据
			// 二开配置项数据
			let globalData = vm.globalData.ds[0];
			vm.$popup.showFormApprove({
				globalData: globalData,
				keyName: vm.keyName,
				keyValue: vm.keyValue,
				ApprovalID: vm.ApprovalID,
				ApprovalSort: vm.ApprovalSort,
				Status: vm.formData.Status,
				confirm: (data) => { // 确认按钮
					// 刷新页面
					vm.refreshPage();
					// vm.initConfig(vm.detailName);
					// 编辑页触发详情页刷新
					// uni.$emit('FormInitToRefresh', {
					// 	keyID: vm.formData[vm.keyName]
					// })
				},
				doneConfirm: (data) => { // 触发按钮的加载完事件脚本
					let workConfig = vm.approvalConfig.workConfig

					let work = workConfig[data];

					var script = work.script.replace(/\['/g, "vm.formData['");
					script = script.replace(/\["/g, "vm.formData[\"");
					eval(script);
					// 刷新页面
					vm.refreshPage();
					// vm.initConfig(vm.detailName);
					// // 编辑页触发详情页刷新
					// uni.$emit('FormInitToRefresh', {
					// 	keyID: vm.formData[vm.keyName]
					// });

					// 打开下一张单据
					vm.openNextPage();

				},
				cancel: (data) => {
					vm.enabledSubmitBtn = true;

				}
			})
		},
		cancelApprove: function(vm) { // 取消审批
			let param = {
				tableName: vm.physicalTableName,
				keyName: vm.keyName,
				keyId: vm.keyValue,
				userId: vm.$store.state.UserInfo.UserID,
			};

			vm.Request.requestByUrl("/Approve/cancelApproval", param, function(data) {

				if (data.ds[0]['Status'] == 200) {
					uni.showToast({
						title: '撤销审批成功'
					});
					// 刷新页面
					vm.refreshPage();
					// vm.initConfig(vm.detailName);
					// // 编辑页触发详情页刷新
					// uni.$emit('FormInitToRefresh', {
					// 	keyID: vm.formData[vm.keyName]
					// })
				} else if (data.ds[0]['Status'] == 500) {
					layer.msg(data.ds[0]['msg'])
					vm.enabledSubmitBtn = true;
				}
			})
		},
		viewListApprove: function(vm) { // 查阅审批流
			// 二开配置项数据
			let globalData = vm.globalData.ds[0];
			vm.$popup.showListApprove({
				tableName: vm.physicalTableName,
				globalData: globalData,
				keyName: vm.keyName,
				keyValue: vm.keyValue,
				ApprovalSort: vm.ApprovalSort,
				Status: vm.formData.Status,
				confirm: (data) => { // 确认按钮 

				},
				cancel: (data) => {
					vm.enabledSubmitBtn = true;

				}
			})
		}
	}
}

export default ApprovalFun();